
;; (require 'slime) ;;ERROR 使用这个,会出现打开后tab显示成.需要重新打开
;; (setq lisp-indent-function 'common-lisp-indent-function)
(setq common-lisp-style-default "sbcl") ; "sbcl modern"
;; (setq lisp-indent-function 'common-lisp-indent-function)
;; (slime-setup '(slime-fancy slime-banner))
(setq slime-contribs '(slime-banner slime-fancy slime-company slime-asdf slime-indentation slime-xref-browser slime-highlight-edits slime-mdot-fu ))  ;; slime-repl-ansi-color  
;; (setq slime-lisp-implementations '((ccl ("/home/yao/dl/ccl/lx86cl64" )) ))
;; (setq slime-lisp-implementations '((abcl ("/usr/bin/java" "-jar" "/home/yao/dl/abcl-bin-1.5.0/abcl.jar" )) ))
;; (setq slime-lisp-implementations '((sbcl ("/usr/local/bin/sbcl") :coding-system utf-8-unix :program-args "--load ~/sbclrc"))) ;有问题
;; (setq slime-lisp-implementations '((sbcl ("/usr/local/bin/sbcl" "--load" "/home/yao/.sbclrc")  :coding-system utf-8-unix)))
(setq slime-lisp-implementations '((sbcl ("/usr/local/bin/sbcl" "--core" "/srv/z.core" )  :coding-system utf-8-unix)))


;; "--eval" "'(dribble \"/tmp/z\")'" not ok
;; (require 'w3m-load)
(setq browse-url-browser-function 'w3m)
(setq common-lisp-hyperspec-root "file:/usr/share/doc/hyperspec/")
;; html turn off fill mode
(add-hook 'mhtml-mode-hook 'turn-off-auto-fill)

;; (eval-after-load 'slime-mode
;;   '(progn
;;      (define-key slime-mode-map (kbd "M-i")  'slime-complete-form)))


(defun my-lispy-at ()
  (interactive)
  (lispy--space-unless "\\s-\\|\\s(\\|[:?#]\\|\\\\\\|~\\|,")
  (insert "@"))

(eval-after-load 'lispy-mode
  '(define-key lispy-mode-map (kbd "@") 'my-lispy-at))

(require 'lispy)
(define-key lispy-mode-map (kbd "@") 'my-lispy-at)
;; (setq racket-racket-program "~/racket/bin/racket")
;; ;; (setq racket-raco-program "raco")
;; (add-hook 'racket-mode-hook
;;           (lambda ()
;;             (define-key racket-mode-map (kbd "C-M-i") 'racket-run)))
;; (setq tab-always-indent 'complete) ;; 使用tab自动补全
;; (setq geiser-racket-binary "~/racket/bin/racket")


(add-to-list 'auto-mode-alist '("\\.wxss$" . css-mode))
(add-to-list 'auto-mode-alist '("\\.swan$" . mhtml-mode))
(put 'iter 'lisp-indent-function nil)
;; (require 'ezbl)
;(setq inferior-lisp-program "/usr/local/bin/sbcl")


(eval-after-load "slime"  ; 'slime-mode 不起作用
  '(define-key slime-mode-map (kbd "C-c C-c")
     'slime-eval-defun
     ;; 'slime-compile-and-load-file
     ;; 'slime-load-file
     ))

(add-hook 'emacs-lisp-mode-hook (lambda () (lispy-mode 1)))
(add-hook 'lisp-mode-hook (lambda () (lispy-mode 1)))
(add-hook 'slime-repl-mode-hook (lambda () (lispy-mode 1)))

(setq lispy-colon-p nil)                ; :前面不加空格



(setq slime-startup-animation t)        ;动画
(setq slime-header-line-p t)          ;最上面一行,不然显示普通buffer样式,更难看

(defun slime-local ()
  "slime-connect 失败会直接退出,不会运行到下面的循环"
  (interactive )
  (let ((conn (slime-connect "localhost" 4006)))
    (while (not (slime-connection-name conn))
      (sleep-for 0 100))
    (let ((repl-name (concat "*slime-repl "
                             (slime-connection-name conn)
                             "*")))
      (switch-to-buffer repl-name)
      (rename-buffer (format "*本地 %s*" (slime-lisp-implementation-type))  t)
      ;; (if (and (string-match-p "slime-repl" (buffer-name (current-buffer))) (equal "slime-repl-mode" (buffer-mode)))
      ;;     (progn (message (buffer-name (current-buffer))) (rename-buffer "*本地 sbcl*" t))
      ;;   (message (buffer-name (current-buffer)))
      ;;   )
      )))

(defun slime-server ()
  (interactive)
  (let ((conn (slime-connect "localhost" 4005)))
    (while (not (slime-connection-name conn))
      (sleep-for 0 100))
    (let ((repl-name (concat "*slime-repl "
                             (slime-connection-name conn)
                             "*")))
      (switch-to-buffer repl-name)
      (rename-buffer (format "*远程 %s*" (slime-lisp-implementation-type))  t)
      )))

(defun slime-f2 ()
  (interactive)
  (let ((conn (slime-connect "localhost" 4007)))
    (while (not (slime-connection-name conn))
      (sleep-for 0 100))
    (let ((repl-name (concat "*slime-repl "
                             (slime-connection-name conn)
                             "*")))
      (switch-to-buffer repl-name)
      (rename-buffer (format "*本地Debian %s*" (slime-lisp-implementation-type))  t)
      )))

(defun slime-f3 ()
  (interactive)
  (let ((conn (slime-connect "localhost" 4003)))
    (while (not (slime-connection-name conn))
      (sleep-for 0 100))
    (let ((repl-name (concat "*slime-repl "
                             (slime-connection-name conn)
                             "*")))
      (switch-to-buffer repl-name)
      (rename-buffer (format "*本地Debian %s*" (slime-lisp-implementation-type))  t)
      )))

(defun slime-rename ()
  (interactive)
  (if (string-match-p "sbcl" (buffer-name (current-buffer)))
      (if (equal 4005 (slime-connection-port (slime-current-connection)))
          (rename-buffer "远程 SBCL" t)
        (rename-buffer "本地 SBCL" t)
        )))

(add-hook 'auto-save-hook 'slime-repl-save-merged-history)

;; (global-whitespace-mode nil)
;; (add-hook 'text-mode-hook 'my-text-hook-function)
;; (setq whitespace-mode-disable '(lisp-mode emacs-lisp-mode))
(if (file-exists-p "~/quicklisp/log4slime-setup.el")
    (progn
      (load "~/quicklisp/log4slime-setup.el")
      (global-log4slime-mode 1))
  (error "log4slime not install!")
  )
;; (global-set-key (kbd "s-p") 'slime-repl-previous-input)

(setq slime-company-completion 'fuzzy
      slime-company-after-completion 'slime-company-just-one-space)

(provide 'slime-config)
